// ignore_for_file: constant_identifier_names

import 'host_config.dart';

/// 网络环境
enum NetEnvironment {
  DEV,
  PROD;

  static NetEnvironment rawValue(String code) =>
      NetEnvironment.values.firstWhere((element) => element.name == code,
          orElse: () => NetEnvironment.DEV);
}

/// 网络配置
class HMNetworkConfig {
  static var _environment = NetEnvironment.DEV;
  static HostType? _host;
  static var _openLog = false;
  static var _proxy = "";
  static var _openMock = false;

  /// 配置
  static config(
    NetEnvironment environment, {
    required HostType host,
    proxy = "",
    openLog = true,
    openMock = false,
    String? token,
  }) {
    _environment = environment;
    _host = host;
    _proxy = proxy;
    _openLog = openLog;
    _openMock = openMock;
    HMNetworkConfig.token = token;
  }

  /// 当前环境
  static NetEnvironment get environment => _environment;

  /// 打开日志？
  static bool get openLog => _openLog;

  /// 代理
  static String get proxy => _proxy;

  /// 启用mock数据？
  static bool get openMock => _openMock;

  /// host
  static HostType get host {
    assert(
        _host != null, "当前没有配置网络环境，你应该在入口函数中使用'HMNetworkConfig.config'进行配置！");
    return _host!;
  }

  /// token
  static String? token;
}

/// host适配
abstract class HostAdaptor<T> {
  late T DEV;
  late T PROD;
  T get current {
    switch (HMNetworkConfig.environment) {
      case NetEnvironment.DEV:
        return DEV;
      case NetEnvironment.PROD:
        return PROD;
      default:
        return DEV;
    }
  }
}

/// host模板类
class HostTemplate extends HostAdaptor<String> {
  @override
  final String DEV;

  @override
  final String PROD;

  HostTemplate({
    required this.DEV,
    required this.PROD,
  });
}
